package project.ccso.top.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.mapper.Mapper;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.PageBean;
import project.ccso.top.bean.ReturnResult;
import project.ccso.top.entity.School;
import project.ccso.top.enumerate.Result;
import project.ccso.top.service.SchoolService;
import project.ccso.top.service.impl.SchoolServiceImpl;

/**
 * SchoolServlet 处理与School相关的请求
 * 
 * @author aibinxiao
 * @date 2018年8月23日 下午3:13:09
 */
@WebServlet("/SchoolServlet")
public class SchoolServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(SchoolServlet.class);
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	private SchoolService schoolService = new SchoolServiceImpl();

	public void findSchoolByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		String schoolId = request.getParameter("id");
		String schoolName = request.getParameter("name");
		String provinceId = request.getParameter("provinceId");
		String cityId = request.getParameter("cityId");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		GridPageBean<School> pageBean = null;
		try {
			pageBean = schoolService.findSchoolByPage(pageNo, pageSize, schoolId, schoolName, provinceId, cityId,
					startDate, endDate);
			result.setResult(project.ccso.top.enumerate.Result.SUCCESS.name());
			result.setMessage("查询第" + pageNo + "页的学校信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(pageBean);
		writer.print(mapper.writeValueAsString(result));

	}

	/**
	 * 根据cityId查询所有学校*
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void selectByCityId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String cityId = request.getParameter("cityId");
		List<School> list = null;
		try {
			list = schoolService.selectByCityId(cityId);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询一个城市所有高校成功");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(list);
		writer.print(mapper.writeValueAsString(result));
	}

}
